package com.lanou.utp.controller;

import com.lanou.utp.entity.*;
import com.lanou.utp.service.CollegeService;
import com.lanou.utp.utils.IdAndName;
import com.lanou.utp.entity.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author MaQingYun
 */
@Api(tags = "组织管理")
@Transactional
@RestController
public class CollegeController {

    @Resource
    private CollegeService collegeService;

    @ApiOperation("获取学院,专业,班级")
    @GetMapping("/school/7/orgs")
    public ResponseData getOrgs() {
        School orgs = this.collegeService.getOrgs();
        return new ResponseData("ok", orgs.getChildren());
    }

    @ApiOperation("根据level和id 查询负责人")
    @GetMapping("/org/{level}/{id}/leaders")
    public ResponseData getLeader(@PathVariable(value = "level") Integer level, @PathVariable("id") Long id) {
        List<IdAndName> getLeader = this.collegeService.getLeader(level, id);
        return new ResponseData("ok", getLeader);
    }

    @ApiOperation("获取学校的所有学院信息")
    @GetMapping("/school/7/colleges")
    public ResponseData getCollegesWithSchool(){
        List<College> collegeList = this.collegeService.getCollegesWithSchool();
        return new ResponseData(collegeList);
    }

    @PostMapping("/org/addCollege")
    public ResponseData addCollege(@RequestBody @Validated(OrgCommonProperty.addCollege.class) OrgCommonProperty parameter) {
        this.collegeService.addCollege(parameter);
        return new ResponseData("ok");
    }

    @GetMapping("/college/{id}/majors")
    public ResponseData getMajors(@PathVariable("id") Long id) {
        List<Major> majors = this.collegeService.getMajors(id);
        return new ResponseData(majors);
    }

    @ApiOperation("更新学院,专业,班级的信息")
    @PutMapping("/org/upCollege")
    public ResponseData updateOrganization(@RequestBody @Validated(OrgCommonProperty.updateCollege.class) OrgCommonProperty parameter) {
        System.out.println("parameter = [" + parameter + "]");
        this.collegeService.updateOrganization(parameter);
        return new ResponseData("ok");
    }

    @ApiOperation("逻辑删除学院,专业, 班级的信息")
    @DeleteMapping("/org/delCollege")
    public ResponseData deleteOrganization(@RequestBody @Validated(OrgCommonProperty.updateCollege.class) OrgCommonProperty parameter) {
        System.out.println("parameter = [" + parameter + "]");
        this.collegeService.deleteOrganization(parameter);
        return new ResponseData("ok");
    }



}
